package com.example.safeassistan.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.safeassistan.R;
import com.example.safeassistan.base.BaseActivity;
import com.example.safeassistan.constants.UrlConstants;
import com.example.safeassistan.home.activity.DeviceInfoDetail;
import com.example.safeassistan.home.adapter.ImageListAdapter;
import com.example.safeassistan.home.model.DeviceDetailModel;
import com.example.safeassistan.http.HttpRequest;
import com.example.safeassistan.http.HttpStringCallBack;
import com.example.safeassistan.my.model.FeedBackDetailModel;
import com.example.safeassistan.utile.JsonUtils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ActivityfeedBackDetail  extends BaseActivity {
    private TextView tvFeedBackPerson,tvFeedBackDepart,tvHandlePerson,tvFeedBackInfo,
            tvHandleResult,tvStatus;
    private RecyclerView rcPic;
    private ImageListAdapter adapter;
    private String id="";
    private Integer type=0;//0 未处理 1已处理

    private FeedBackDetailModel model;
    public static void actionStart(Context context,String id,Integer type) {
        Intent intent = new Intent(context, ActivityfeedBackDetail.class);
        intent.putExtra("id",id);
        intent.putExtra("type",type);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setAndroidNativeLightStatusBar(true);
    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.feedback_detail;
    }

    @Override
    protected void initView() {
        setTitleText("反馈详情");
        id=getIntent().getStringExtra("id");
        type=getIntent().getIntExtra("type",0);
        rcPic=findViewById(R.id.rcPic);
        tvStatus=findViewById(R.id.tvStatus);
        tvHandleResult=findViewById(R.id.tvHandleResult);
        tvFeedBackDepart=findViewById(R.id.tvFeedBackDepart);
        tvFeedBackPerson=findViewById(R.id.tvFeedBackPerson);
        tvHandlePerson=findViewById(R.id.tvHandlePerson);
        tvFeedBackInfo=findViewById(R.id.tvFeedBackInfo);
        getFeedBackDetail();
    }


    public void getFeedBackDetail(){
        Map map = new HashMap<>();
        map.put("id", id);
        new HttpRequest(this).doGet(UrlConstants.queryFeedbackListById, null, map, new HttpStringCallBack() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onSuccess(Object result) {
                String datas = result.toString();
                JSONObject jsonObject = null;
                try {
                    jsonObject = new JSONObject(datas);
                    String message = jsonObject.getString("message");
                    Boolean success = jsonObject.getBoolean("success");
                    if (success) {
                        Object obj = JsonUtils.fromJson(result.toString(), FeedBackDetailModel.class);
                        if (obj instanceof FeedBackDetailModel){
                            model=(FeedBackDetailModel)obj;
                            setView();
                        }
                    }else {
                        Toast.makeText(ActivityfeedBackDetail.this,message,Toast.LENGTH_SHORT).show();
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(int code, String msg) { }
        });
    }

    public void setView(){
        tvFeedBackPerson.setText(model.getResult().getExecutorUserName());
        tvFeedBackDepart.setText("【"+model.getResult().getExecutorGroupName()+"】");
        tvHandlePerson.setText(model.getResult().getDisposeUserName());
        tvFeedBackInfo.setText(model.getResult().getSpotRemark());
        tvHandleResult.setText(model.getResult().getResultRemark());

        if (type==0){
            tvStatus.setTextColor(getResources().getColor(R.color.orange));
            tvStatus.setBackgroundResource(R.drawable.stoke_orange_bg);
            tvStatus.setText("未处理");
        }else {
            tvStatus.setTextColor(getResources().getColor(R.color.green));
            tvStatus.setBackgroundResource(R.drawable.stoke_green_bg);
            tvStatus.setText("已处理");
        }


        if (model.getResult().getSceneImg().length()>0){
            String str []=model.getResult().getSceneImg().split(",");
            List<String> imgs=new ArrayList<>();

            for (int i=0;i<str.length;i++){
                if (str[i].length()>0&&!str[i].equals(""))
                imgs.add(str[i]);
            }
            adapter=new ImageListAdapter(imgs);
            rcPic.setAdapter(adapter);
            rcPic.setLayoutManager(new GridLayoutManager(ActivityfeedBackDetail.this,3,RecyclerView.VERTICAL,false));
        }

    }

    @Override
    protected void initData() { }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}
